package com.soshalboard.controller;

import java.util.List;
import java.util.Map;

import javax.inject.Inject;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.gson.Gson;
import com.soshalboard.persistence.domain.Category;
import com.soshalboard.persistence.domain.Product;
import com.soshalboard.persistence.domain.Session;
import com.soshalboard.persistence.domain.WishList;
import com.soshalboard.remote.SessionManager;
import com.soshalboard.remote.interfaces.APIInterface;

@Controller
@RequestMapping(value = "/product")
public class ProductController {

	@Inject
	SessionManager sessionManager;	
	
	@Inject
	APIInterface api;
	
	Gson gson = new Gson();
	
	@RequestMapping(value = "/{session_id}", method = RequestMethod.POST)
    @ResponseBody
	public ResponseEntity<String> findCategories(@PathVariable("session_id") final String sessionId, @RequestBody String json) {
	    ResponseEntity<String> responseEntity = null;
	    Session session = checkSession(sessionId);
	    if (null == session) 
	        return new ResponseEntity<String>("Not Authorised", HttpStatus.UNAUTHORIZED);
	    
	    Integer userId = session.getUserId();
	    Product product = gson.fromJson(json, Product.class);
	    product.setCreator(userId);
	    
	    try {
	    	api.createOrUpdateProduct(product);
	        responseEntity = new ResponseEntity<String>("Successful", HttpStatus.OK);
	    } catch (Exception Ex) {
	        responseEntity = new ResponseEntity<String>("Object Not Found", HttpStatus.NOT_FOUND);
	    }
	    
	    return responseEntity;
	}		
	
	@RequestMapping(value = "/{session_id}/{category_id}", method = RequestMethod.GET)
    @ResponseBody
	public ResponseEntity<String> findProductBasedOnCategory(@PathVariable("session_id") final String sessionId, @PathVariable("category_id") final Integer categoryId) {
	    ResponseEntity<String> responseEntity = null;
	    Session session = checkSession(sessionId);
	    if (null == session) 
	        return new ResponseEntity<String>("Not Authorised", HttpStatus.UNAUTHORIZED);

    	List<Product> product = api.getProductsByCategory(categoryId);

    	
	    try {
	        responseEntity = new ResponseEntity<String>("Successful", HttpStatus.OK);
	    } catch (Exception Ex) {
	        responseEntity = new ResponseEntity<String>("Object Not Found", HttpStatus.NOT_FOUND);
	    }
	    
	    return responseEntity;
	}		
	
	public Session checkSession(String sessionId) {
		if (sessionManager.getSessionMap().containsKey(sessionId)) {
			return sessionManager.getSessionMap().get(sessionId);
		} else {
			Session session = api.getSession(sessionId);
			return session;
		}
	}			
}
